﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model
{
    /// <summary>
    /// RESPONSABILITY: - company repository
    ///                 - create new company
    ///                 - add company to the list of companies
    ///                 - change company code
    ///                 - change comapny description
    /// </summary>
    public class ChangeCompany
    {
        private List<Company> companies = new List<Company>();


        public IEnumerable<Company> GetCompanies(List<string> codes = null)
        {
            if (codes == null){
                return companies;
            }

            var result = companies.Where(c => codes.Contains(c.CompanyCode));
            return result;
        }

        // create new company
        public Company CreateCompany(string companyCode, string companyDescription) 
        {
            if (string.IsNullOrEmpty(companyCode))
            {
                throw new ArgumentException("The company code cannot be null", companyCode);
            }

            Company company = new Company(companyCode, companyDescription);
            this.AddCompany(company);
            return company;
        }

        //add the company to the company list
        public void AddCompany(Company company)
        {
            //exista acelasi id?
            if(CheckCompanyIDExistance(company.CompanyId)){
                throw new Exception("A company with the id " + company.CompanyId + " already exists");
            }
            //exista acelasi cod?
            if(CheckCompanyCodeExistance(company.CompanyCode)){
                throw new Exception("A company with the code " + company.CompanyCode + " already exists");
            }

            companies.Add(company);
            
        }


        private bool CheckCompanyCodeExistance(string code)
        {
            var _company = companies.FirstOrDefault(p => p.CompanyCode == code);
            
            var isFound = _company != null;

            return isFound;
        }

        private bool CheckCompanyIDExistance(int id)
        {
            
            var _company = companies.FirstOrDefault(p => p.CompanyId == id);

            var isFound = _company != null;

            return isFound;
        }

        //delete a company 
        public void DeleteCompany(Company company)
        {
           Company deleteCompany = companies.FirstOrDefault(p => p.CompanyId == company.CompanyId);
            if (deleteCompany != null)
            {
                companies.Remove(company);
            }
        }


        public void ChangeCompanyCode(Company company, string newCompanyCode)
        {
            Company foundCompany = companies.FirstOrDefault(c => c.CompanyId == company.CompanyId);
            if (foundCompany != null)
                company.CompanyCode = newCompanyCode;
        }

        public void ChangeCompanyDescription(Company company, string newCompanyDescription)
        {

            Company foundCompany = companies.FirstOrDefault(c => c.CompanyId == company.CompanyId);
            if (foundCompany != null)
            {
                company.CompanyDescription = newCompanyDescription;
            }
        } 
    }
}

